<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" import="Dao.LUDAO"
	import="java.util.Hashtable" import="DataClasses.*" import="Ranking.*"
	import="java.util.ArrayList" import="java.util.List"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 5.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html {
	height: 100%
}

body {
	height: 100%;
	margin: 0;
	padding: 0
}

#map_canvas {
	height: 100%
}
</style>
<script type="text/javascript"
	src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAZgtP0A9fPfhqKg7sz9pAJ_etOXCCbPpg&sensor=false">
	
</script>

<%
	LUDAO db = new LUDAO();
	
	Ranking r = new Ranking();
	r.transform(db);
	r.scoreAll();
	List<House1> houseList = r.getHouse();
	String latArrayFac = "", longArrayFac = "", nameArrayFac = "", typeArrayFac = "";
	
	String rentRange=request.getParameter("rentRG");
	//out.print(rentRange);
	String buyRange =request.getParameter("buyRG");//out.print(buyRange);
	//String searchBar = request.getParameter("bar");out.print(searchBar);
	String searchBar = " ";
	String buildingTypeList = request.getParameter("bt");//out.print(buildingTypeList);
	
	String schoolFilter = request.getParameter("inSchool") == null ? "" : request.getParameter("inSchool");//out.print(schoolFilter);
	String shopFilter =request.getParameter("inShop") == null ? "" : request.getParameter("inShop");//out.print(shopFilter);
	String gasFilter=request.getParameter("inGas") == null ? "" : request.getParameter("inGas");//out.print(gasFilter);
	String restFilter = request.getParameter("inRest") == null ? "" : request.getParameter("inRest");//out.print(restFilter);
	String subFilter = request.getParameter("inSub") == null ? "" : request.getParameter("inSub");//out.print(subFilter);
	
	if(schoolFilter.equals("on")){
	Hashtable<Integer, Object> schools_ht = db.getSchool();
	for (Object school : schools_ht.values()){
		latArrayFac = latArrayFac + ((School)school).getLatitude() + ";";
		longArrayFac = longArrayFac + ((School)school).getLongitude() + ";";
		nameArrayFac = nameArrayFac + ((School)school).getName() + ";";
		typeArrayFac = typeArrayFac + "School;";
	}}
	
	
	if(restFilter.equals("on")){
	Hashtable<Integer, Object> restaurant_ht = db.getRestaurant();
	for (Object restaurant : restaurant_ht.values()){
		latArrayFac = latArrayFac + ((Restaurant)restaurant).getLatitude() + ";";
		longArrayFac = longArrayFac + ((Restaurant)restaurant).getLongitude() + ";";
		nameArrayFac = nameArrayFac + ((Restaurant)restaurant).getName() + ";";
		typeArrayFac = typeArrayFac + "Restaurant;";
	}}
	
	if(subFilter.equals("on")){
	Hashtable<Integer, Object> transp_ht = db.getTransportations();
	for (Object transp : transp_ht.values()){
		latArrayFac = latArrayFac + ((Transportation) transp).getLatitude() + ";";
		longArrayFac = longArrayFac + ((Transportation) transp).getLongitude() + ";";
		nameArrayFac = nameArrayFac + ((Transportation) transp).getName() + ";";
		typeArrayFac = typeArrayFac + "Subway;";
	}}
	
	if(shopFilter.equals("on")){
	Hashtable<Integer, Object> shop_ht = db.getShop();
	for (Object shop : shop_ht.values()){
		latArrayFac = latArrayFac + ((Shop) shop).getLatitude() + ";";
		longArrayFac = longArrayFac + ((Shop) shop).getLongitude() + ";";
		nameArrayFac = nameArrayFac + ((Shop) shop).getName() + ";";
		typeArrayFac = typeArrayFac + "Shop;";
	}}
	
	String latArrayHsg = "", longArrayHsg = "", nameArrayHsg = "", typeArrayHsg = "";
	String resArrayHsg = "", schArrayHsg = "", shpArrayHsg = "", gasArrayHsg = "", sbwArrayHsg = "", scrArrayHsg = "";
	String priceArray = "", bedroomArray = "", parkingArray = "";
	for (House1 hse : houseList){
		latArrayHsg = latArrayHsg + ( (House1)hse).latitude + ";";
		longArrayHsg = longArrayHsg + ( (House1)hse).longitude + ";";
		nameArrayHsg = nameArrayHsg + ( (House1)hse).name + ";";
		typeArrayHsg = typeArrayHsg + "Condo;";
		resArrayHsg = resArrayHsg + ( (House1)hse).resteraunt + ";";
		schArrayHsg = schArrayHsg + ( (House1)hse).school + ";";
		shpArrayHsg = shpArrayHsg + ( (House1)hse).shop + ";";
		gasArrayHsg = gasArrayHsg + ( (House1)hse).gas + ";";
		sbwArrayHsg = sbwArrayHsg + ( (House1)hse).subway + ";";
		scrArrayHsg = scrArrayHsg + ( (House1)hse).score + ";";
		priceArray = priceArray + ( (House1)hse).price + ";";
		bedroomArray = bedroomArray + ( (House1)hse).bedroom + ";";
		parkingArray = parkingArray + ( (House1)hse).parking + ";";
	}
	
	
	
	
	//out.println("<br>"+nameArray);
%>

<script type="text/javascript">
    
	var map_obj = null;
	var NA = 'n/a';
	
	var infowindow = new google.maps.InfoWindow({
	    size: new google.maps.Size(150, 50)
	});
	
	function callMap(latArrayFac, longArrayFac, nameArrayFac, typeArrayFac,
			latArrayHsg, longArrayHsg, nameArrayHsg, typeArrayHsg,resArrayHsg,
			schArrayHsg,shpArrayHsg,gasArrayHsg,sbwArrayHsg,scrArrayHsg,
			priceArray,bedroomArray,parkingArray,rentRange,buyRange,searchBar,
			buildingTypeList,schoolFilter,shopFilter,gasFilter,restFilter,
			subFilter ) {

		var mapOptions = {
			center : new google.maps.LatLng(43.6481, -79.4042),
			zoom : 15,
			mapTypeControl: true,
			mapTypeControlOptions: {
	            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
	        },
	        navigationControl: true,
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};
		
		map_obj = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
		
		google.maps.event.addListener(map_obj, 'click', function() {
	        infowindow.close();
	    });

		var latStrArrayFac = latArrayFac.split(";");
		var longtiStrArrayFac = longArrayFac.split(";");
		var nameStrArrayFac = nameArrayFac.split(";");
		var typeStrArrayFac = typeArrayFac.split(";");

		var sizeFac = latStrArrayFac.length;
		
	    // Add markers to the map
	    // Set up markers based on the number of elements within the myPoints array
	    for (var i = 0; i < sizeFac-1; i++) {
	    	var lat = parseFloat(latStrArrayFac[i]);
			var longti = parseFloat(longtiStrArrayFac[i]);
			var name = nameStrArrayFac[i];
			var type = typeStrArrayFac[i];
			var newPlace = new place(lat, longti, name,type,NA,NA,NA,NA,NA,NA,NA,NA,NA);
	        addMarker(new google.maps.LatLng(lat, longti), name, newPlace);
	    }
	    
	    var latStrArrayHsg = latArrayHsg.split(";");
		var longtiStrArrayHsg = longArrayHsg.split(";");
		var nameStrArrayHsg = nameArrayHsg.split(";");
		var typeStrArrayHsg = typeArrayHsg.split(";");
		var resStrArrayHsg = resArrayHsg.split(";");
		var schStrArrayHsg = schArrayHsg.split(";");
		var shpStrArrayHsg = shpArrayHsg.split(";");
		var gasStrArrayHsg = gasArrayHsg.split(";");
		var sbwStrArrayHsg = sbwArrayHsg.split(";");
		var scrStrArrayHsg = scrArrayHsg.split(";");
		var priceStrArray = priceArray.split(";");
		var bedroomStrArray = bedroomArray.split(";");
		var parkingStrArray = parkingArray.split(";");
		
		var sizeHsg = latStrArrayHsg.length;
	    
		for (var i = 0; i < sizeHsg-1; i++) {
	    	var lat = parseFloat(latStrArrayHsg[i]);
			var longti = parseFloat(longtiStrArrayHsg[i]);
			var name = nameStrArrayHsg[i];
			var type = typeStrArrayHsg[i];
			var newPlace = new place(lat, longti, name,type,resStrArrayHsg[i],shpStrArrayHsg[i],schStrArrayHsg[i],sbwStrArrayHsg[i],gasStrArrayHsg[i],scrStrArrayHsg[i],priceStrArray[i],bedroomStrArray[i],parkingStrArray[i]);
	        addMarker2(new google.maps.LatLng(lat, longti), name, newPlace);
	    }
	    
	}

	function addMarker(latlng, myTitle, newPlace) {
	    var contentString = createContStr(newPlace);
	    var marker = new google.maps.Marker({
	        position: latlng,
	        map: map_obj,
	        title: myTitle,
	        icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
	    });

	    google.maps.event.addListener(marker, 'click', function() {
	        infowindow.setContent(contentString);
	        infowindow.open(map_obj, marker);
	    });
	}
	
	function addMarker2(latlng, myTitle, newPlace) {
	    var contentString = createContStr(newPlace);
	    
	    if(newPlace.total_rate>=2){
	    var marker = new google.maps.Marker({
	        position: latlng,
	        map: map_obj,
	        title: myTitle,
	    });

	    google.maps.event.addListener(marker, 'click', function() {
	        infowindow.setContent(contentString);
	        infowindow.open(map_obj, marker);
	    });
	}
	}

	function createContStr(place) {
		var description = place.type;

		str = '<h3 class="firstHeading">' + place.name + '</h3>' + 
		'<p>'+ description + '</p>' + 
		'<p> Price: '+ place.price + '</p>' +
		//'<p> Bedroom: '+ place.bedroom + '</p>' + 
		//'<p> Parking: '+ place.parking + '</p>' + 
		'<p> Score based on Restaurant: '+ place.food_rate + '</p>' + 
		'<p> Score based on Shopping Malls: '+ place.shop_rate + '</p>' +
		'<p> Score based on Education: '+ place.edu_rate + '</p>' +
		'<p> Score based on Subway Avail.: '+ place.trans_rate + '</p>' +
		'<p> Score based on Gas Station: '+ place.gas_rate + '</p>' +
		'<p> Total score: '+ place.gas_rate + '</p>'
		;

		return str;
	}

	// Place JS object
	function place(lat, longd, name,type,food_rate,shop_rate,edu_rate,trans_rate,gas_rate,total_rate,price,bedroom,parking) {
		this.lat = lat;
		this.longd = longd;
		this.name = name;
		this.type = type;
		this.food_rate = food_rate;
		this.shop_rate = shop_rate;
		this.edu_rate = edu_rate;
		this.trans_rate = trans_rate;
		this.gas_rate = gas_rate;
		this.total_rate = total_rate;
		this.price = price;
		this.bedroom = bedroom;
		this.parking = parking;

		return this;
	}
</script>

</head>
<body onload="callMap('<%out.print(latArrayFac);%>',
			'<%out.print(longArrayFac);%>','<%out.print(nameArrayFac);%>',
			'<%out.print(typeArrayFac);%>',
			'<%out.print(latArrayHsg);%>','<%out.print(longArrayHsg);%>',
			'<%out.print(nameArrayHsg);%>','<%out.print(typeArrayHsg);%>',
			'<%out.print(resArrayHsg);%>','<%out.print(schArrayHsg);%>',
			'<%out.print(shpArrayHsg);%>','<%out.print(gasArrayHsg);%>',
			'<%out.print(sbwArrayHsg);%>','<%out.print(scrArrayHsg);%>',
			'<%out.print(priceArray);%>','<%out.print(bedroomArray);%>',
			'<%out.print(parkingArray);%>',
			'<%out.print(rentRange);%>','<%out.print(buyRange);%>',
			'<%out.print(searchBar);%>','<%out.print(buildingTypeList);%>',
			'<%out.print(schoolFilter);%>','<%out.print(shopFilter);%>',
			'<%out.print(gasFilter);%>','<%out.print(restFilter);%>',
			'<%out.print(subFilter);%>') ">

<%-- 	<div>
		<button type="button"
			onclick="callMap('<%out.print(latArrayFac);%>',
			'<%out.print(longArrayFac);%>','<%out.print(nameArrayFac);%>',
			'<%out.print(typeArrayFac);%>',
			'<%out.print(latArrayHsg);%>','<%out.print(longArrayHsg);%>',
			'<%out.print(nameArrayHsg);%>','<%out.print(typeArrayHsg);%>',
			'<%out.print(resArrayHsg);%>','<%out.print(schArrayHsg);%>',
			'<%out.print(shpArrayHsg);%>','<%out.print(gasArrayHsg);%>',
			'<%out.print(sbwArrayHsg);%>','<%out.print(scrArrayHsg);%>',
			'<%out.print(priceArray);%>','<%out.print(bedroomArray);%>',
			'<%out.print(parkingArray);%>',
			'<%out.print(rentRange);%>','<%out.print(buyRange);%>',
			'<%out.print(searchBar);%>','<%out.print(buildingTypeList);%>',
			'<%out.print(schoolFilter);%>','<%out.print(shopFilter);%>',
			'<%out.print(gasFilter);%>','<%out.print(restFilter);%>',
			'<%out.print(subFilter);%>') ">
			View Map</button>
	</div> --%>
	
	
	<div id="map_canvas" style="width: 80%; height: 80%"></div>
</body>
</html>